﻿// Copyright (c) 2010
// by http://openlightgroup.net/

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
using System.Collections.Generic;
using SilverlightDebateViewModel.SilverlightDebateWS;

namespace SilverlightDebateViewModel
{
    public class MainPageViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        ValidationHandler validationHandler = new ValidationHandler();

        public MainPageViewModel()
        {
            // Set the command property
            BaseAddressCommand = new DelegateCommand(SetBaseAddress, CanSetBaseAddress);
            RIAAuthenticationHeaderCommand = new DelegateCommand(SetRIAAuthenticationHeader, CanSetRIAAuthenticationHeader);
            SetSelectedStatementToOpenCommand = new DelegateCommand(SetSelectedStatementToOpen, CanSetSelectedStatementToOpen);
            SetReturnICommandNewOpinionCommand = new DelegateCommand(SetReturnICommandNewOpinion, CanSetReturnICommandNewOpinion);
            CreateNewStatementCommand = new DelegateCommand(CreateNewStatement, CanCreateNewStatement);
            SetSelectedCategoryCommand = new DelegateCommand(SetSelectedCategory, CanSetSelectedCategory);

            // The following line prevents Expression Blend
            // from showing an error when in design mode
            if (!DesignerProperties.IsInDesignTool)
            {
                VMStatementsViewModel = new StatementsViewModel();
                VMStatementsViewModel.VMMainPageViewModel = this;
                VMStatementsViewModel.Visibility_Statements = Visibility.Collapsed;
            }
        }

        // Commands

        #region SetSelectedStatementToOpenCommand
        public ICommand SetSelectedStatementToOpenCommand { get; set; }
        public void SetSelectedStatementToOpen(object param)
        {
            if (param != null && param is Statement)
            {
                SelectedStatementToOpen = (Statement)param;
            }
            else
            {
                SelectedStatementToOpen = null;
            }
        }

        private bool CanSetSelectedStatementToOpen(object param)
        {
            return ((param as Statement) != null);
        }
        #endregion

        #region SetReturnICommandNewOpinion
        public ICommand SetReturnICommandNewOpinionCommand { get; set; }
        public void SetReturnICommandNewOpinion(object param)
        {
            ReturnICommandNewOpinion = (ICommand)param;
        }

        private bool CanSetReturnICommandNewOpinion(object param)
        {
            return ((param as ICommand) != null);
        }
        #endregion

        #region SetSelectedCategoryCmd
        public ICommand SetSelectedCategoryCommand { get; set; }
        public void SetSelectedCategory(object param)
        {
            SelectedCategory = (Category)param;
        }
        private bool CanSetSelectedCategory(object param)
        {
            return true;
        }
        #endregion

        #region CreateNewStatementCommand
        public ICommand CreateNewStatementCommand { get; set; }
        private bool CanCreateNewStatement(object param)
        {
            return true;
        }
        public void CreateNewStatement(object param)
        {
            string baseAddress = this.BaseAddressProperty;
            RIAAuthenticationHeader riaHeader = this.RIAAuthenticationHeaderProperty;

            Statement _statement = param as Statement;

            if (_statement != null)
            {

                DebateModel.CreateNewStatement(_statement.CategoryID, _statement.Title, _statement.StatementContent, riaHeader, baseAddress, (sender, EventArgs) =>
                    {
                        if (EventArgs.Error == null)
                        {

                        }
                        else
                        {
                            //BUGBUG ADD error handling message
                        }
                    });
            }
        }
        #endregion

        #region FlipSwitchShowNewStatement
        public ICommand FlipSwitchShowNewStatementCommand { get; set; }
        private bool CanFlipSwitchShowNewStatement(object param)
        {
            return true;
        }
        public void FlipSwitchShowNewStatement(object param)
        {
            SwitchShowNewStatement = true;
        }
        #endregion

        // Operations

        // Properties - Authentication

        #region BaseAddressProperty
        private string _BaseAddressProperty;
        public string BaseAddressProperty
        {
            get
            {
                return this._BaseAddressProperty;
            }
            set
            {
                this._BaseAddressProperty = value;
                this.NotifyPropertyChanged("BaseAddressProperty");
            }
        }
        #endregion

        #region RIAAuthenticationHeaderProperty
        private RIAAuthenticationHeader _RIAAuthenticationHeaderProperty;
        public RIAAuthenticationHeader RIAAuthenticationHeaderProperty
        {
            get
            {
                return this._RIAAuthenticationHeaderProperty;
            }
            set
            {
                this._RIAAuthenticationHeaderProperty = value;
                this.NotifyPropertyChanged("RIAAuthenticationHeaderProperty");
            }
        }
        #endregion

        #region Message
        private string _Message;
        public string Message
        {
            get { return _Message; }
            private set
            {
                if (Message == value)
                {
                    return;
                }
                _Message = value;
                this.NotifyPropertyChanged("Message");
            }
        }
        #endregion

        #region Username
        private string _Username;
        public string Username
        {
            get { return _Username; }
            private set
            {
                if (Username == value)
                {
                    return;
                }
                _Username = value;
                this.NotifyPropertyChanged("Username");
            }
        }
        #endregion

        #region UserId
        private int _UserId;
        public int UserId
        {
            get { return _UserId; }
            private set
            {
                if (UserId == value)
                {
                    return;
                }
                _UserId = value;
                this.NotifyPropertyChanged("UserId");
            }
        }
        #endregion

        #region RIAKey
        private string _RIAKey;
        public string RIAKey
        {
            get { return _RIAKey; }
            private set
            {
                if (RIAKey == value)
                {
                    return;
                }
                _RIAKey = value;
                this.NotifyPropertyChanged("RIAKey");
            }
        }
        #endregion

        // Statments

        #region SelectedStatementToOpen
        private Statement _SelectedStatementToOpen;
        public Statement SelectedStatementToOpen
        {
            get { return _SelectedStatementToOpen; }
            set
            {
                if (_SelectedStatementToOpen == value)
                {
                    return;
                }
                _SelectedStatementToOpen = value;
                this.NotifyPropertyChanged("SelectedStatementToOpen");
            }
        }
        #endregion

        // ReturnOpinion

        #region ReturnICommandProperty
        private ICommand _ReturnICommandNewOpinion;
        public ICommand ReturnICommandNewOpinion
        {
            get
            {
                return _ReturnICommandNewOpinion;
            }
            set
            {
                if (_ReturnICommandNewOpinion == value)
                {
                    return;
                }
                _ReturnICommandNewOpinion = value;
                this.NotifyPropertyChanged("ReturnICommandNewOpinion");
            }
        }
        #endregion

        #region SelectedCategoryProperty
        private Category _SelectedCategory;
        public Category SelectedCategory
        {
            get
            {
                return _SelectedCategory;
            }
            set
            {
                if (_SelectedCategory == value)
                {
                    return;
                }
                _SelectedCategory = value;
                this.NotifyPropertyChanged("SelectedCategory");
            }
        }
        #endregion

        #region SwitchShowNewStatement
        /// <summary>
        /// When this is changed it will notify main to show new statement window.
        /// </summary>
        private bool _SwitchShowNewStatement;
        public bool SwitchShowNewStatement
        {
            get
            {
                return _SwitchShowNewStatement;
            }
            set
            {
                _SwitchShowNewStatement = value;
                this.NotifyPropertyChanged("SwitchShowNewStatement");
            }

        }
        #endregion

        // Child ViewModels

        #region VMStatementsViewModel
        private StatementsViewModel _VMStatementsViewModel;
        public StatementsViewModel VMStatementsViewModel
        {
            get { return _VMStatementsViewModel; }
            set
            {
                if (VMStatementsViewModel == value)
                {
                    return;
                }
                _VMStatementsViewModel = value;
                this.NotifyPropertyChanged("VMStatementsViewModel");
            }
        }
        #endregion

        // Utility

        #region BaseAddressCommand
        public ICommand BaseAddressCommand { get; set; }
        public void SetBaseAddress(object param)
        {
            BaseAddressProperty = (string)param;
        }

        private bool CanSetBaseAddress(object param)
        {
            return true;
        }
        #endregion

        #region RIAAuthenticationHeaderCommand
        public ICommand RIAAuthenticationHeaderCommand { get; set; }
        public void SetRIAAuthenticationHeader(object param)
        {
            // Get the RIAAuthenticationHeader as a Dictionary
            Dictionary<string, string> Dict_RIAAuthenticationHeader = (Dictionary<string, string>)param;

            // Create an RIAAuthenticationHeader
            RIAAuthenticationHeader AH = new RIAAuthenticationHeader();
            AH.Username = Convert.ToString(Dict_RIAAuthenticationHeader["Username"]);
            AH.UserID = Convert.ToInt32(Dict_RIAAuthenticationHeader["UserID"]);
            AH.Password = Convert.ToString(Dict_RIAAuthenticationHeader["Password"]);
            AH.PortalID = Convert.ToInt32(Dict_RIAAuthenticationHeader["PortalID"]);
            AH.IPAddress = Convert.ToString(Dict_RIAAuthenticationHeader["IPAddress"]);

            RIAAuthenticationHeaderProperty = AH;

            // Set the Username, UserId, and RIAKey
            Username = RIAAuthenticationHeaderProperty.Username;
            UserId = RIAAuthenticationHeaderProperty.UserID;
            RIAKey = RIAAuthenticationHeaderProperty.Password;
        }

        private bool CanSetRIAAuthenticationHeader(object param)
        {
            return true;
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        #region Error Validation
        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                if (this.validationHandler.BrokenRuleExists(columnName))
                {
                    return this.validationHandler[columnName];
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion
    }
}
